<?php
//define('FPDF_FONTPATH', 'C:\wamp\www\dnevnik\web\font\\');
define('FPDF_FONTPATH', '/home/dnevnik/public_html/font/');
class TabelaZaUnos
{
    protected $razreds;
    protected $uceniks;
    protected $model; // ucenici, clanarina, prazno
    protected $broj_redova = 35;

    public function TabelaZaUnos($razreds, $model)
    {
        $this->razreds = $razreds;
        $modeli_obrasca = SkolaPeer::getObrazacTips();
        $this->model = $modeli_obrasca[$model];
        $razred_ids = array();
        foreach($razreds as $razred)
          $razred_ids[] = $razred->getId();

        if($this->model != 'prazno')
        {
           $ucenik_dbs = UcenikPeer::getJoin();
           if($this->model == 'clanarina')
             $ucenik_dbs = $ucenik_dbs->
                           leftJoin('Ucenik.RoditeljId', 'Roditelj.Id')->
                           withColumn('Roditelj.Clanarina', 'Clanarina')->
                           withColumn('Roditelj.ClanarinaTst', 'ClanarinaTst')->
                           withColumn('Roditelj.ClanarinaSoc', 'ClanarinaSoc');
           $ucenik_dbs = $ucenik_dbs->
                           where('RazredId', 'in', $razred_ids)->
                           orderBy('RazredId')->orderBy('RedBr')->
                           orderBy('Prezime')->
                           orderBy('Ime')->
                           find();
           $this->uceniks = array();
           if($this->model == 'ucenici')
             foreach($ucenik_dbs as $ucenik_db)
               $this->uceniks[$ucenik_db->getRazredId()][] = $ucenik_db;
           else if($this->model == 'clanarina')
             foreach($ucenik_dbs as $ucenik_db)
             {
                if(strtotime($ucenik_db->getColumn('Clanarina')) > time() or $ucenik_db->getColumn('ClanarinaTst') or $ucenik_db->getColumn('ClanarinaSoc'))
                 $this->uceniks[$ucenik_db->getRazredId()][] = $ucenik_db;
             }
        }


        $predmet_dbs = DbFinder::from('Predmet')->
                       leftJoin('Predmet.Id', 'PredmetRazred.PredmetId')->
                       leftJoin('PredmetRazred.RazredId', 'Razred.Id')->
                       select(array('Razred.Id', 'Predmet.Id'))->
                       where('Razred.Id', 'in', $razred_ids)->
                       where('Predmet.Tip', '<>', PredmetPeer::$tip['placeholder'])->
                       orderBy('Razred.Id')->
                       orderBy('Predmet.PredmetKratki')->
                       find();

        $this->predmets = array();
        foreach($predmet_dbs as $predmet_db)
          $this->predmets[$predmet_db['Razred.Id']][] = $predmet_db['Predmet.Id'];
        $grupa_dbs = DbFinder::from('Ucenik')->
                     leftJoin('Ucenik.RazredId', 'Razred.Id')->
                     leftJoin('Ucenik.Id', 'UcenikRazred.UcenikId')->
                     leftJoin('Razred RazredSmer', 'UcenikRazred.RazredId', 'RazredSmer.Id')->
                     leftJoin('RazredSmer.SmerId', 'Smer.Id')->
                     leftJoin('Smer.Naziv', 'Predmet.Predmet')->
                     select(array('Razred.Id', 'Predmet.Id'))->
                     where('Razred.Id', 'in', $razred_ids)->
                     groupBy('Razred.Id')->
                     groupBy('Smer.Id')->
                     find();
        foreach($grupa_dbs as $grupa_db)
          if($izborni = $grupa_db['Predmet.Id'])
            $this->predmets[$grupa_db['Razred.Id']][] = $izborni;
        foreach($razreds as $razred)
          if($jezik = $razred->getJezik())
            $this->predmets[$razred->getId()][] = $jezik;

        $this->pdf = $this->pdf();
    }

    public function pdf()
    {
        $pdf = new FPDF('P', 'pt');
        $pdf->SetMargins(0,0,0);
        $pdf->AddFont('Arial', '', 'arial_normal.php');
        $pdf->SetFont('Arial', '', 12);

        foreach($this->razreds as $razred)
        {
           $pdf->AddPage();

           $pdf->Cell(500, 20, iconv('UTF-8', 'Windows-1250', 'III - Obrazac za unos ocena'), '', 0, 'L');
           $pdf->Ln();
           $pdf->Cell(500, 20, iconv('UTF-8', 'Windows-1250', 'Razred: '.$razred.'   Datum: '.date('d.n.Y.')), '', 0, 'L');
           $pdf->Ln();

           $pdf->Cell(20, 100, iconv('UTF-8', 'Windows-1250', 'RB'), 'LBRT', 0, 'L');
           $pdf->Cell(150, 100, iconv('UTF-8', 'Windows-1250', 'Prezime i ime'), 'LBRT', 0, 'L');

           if($predmet_razred = isset($this->predmets[$razred->getId()]) ? $this->predmets[$razred->getId()] : 0)
           foreach($predmet_razred as $predmet)
           {
             $pdf->Cell(20, 100, iconv('UTF-8', 'Windows-1250', $pdf->Image('images/predmeti/'.$predmet.'.jpg', $pdf->GetX(), $pdf->GetY(), 18, 100)), 'LBRT', 0, 'L');
           }

           $pdf->Ln();

           if($this->model == 'ucenici')
           {
              if($ucenik_razred = isset($this->uceniks[$razred->getId()]) ? $this->uceniks[$razred->getId()] : 0)
                 foreach($ucenik_razred as $ucenik)
                 {
                   $pdf->Cell(20, 18, iconv('UTF-8', 'Windows-1250', $ucenik->getRedBr()), 'LBR', 0, 'L');
                   $pdf->Cell(150, 18, iconv('UTF-8', 'Windows-1250', $ucenik->joinIme()), 'LBR', 0, 'L');

                   if($predmet_razred = isset($this->predmets[$razred->getId()]) ? $this->predmets[$razred->getId()] : 0)
                   foreach($predmet_razred as $predmet)
                   {
                     $pdf->Cell(20, 18, iconv('UTF-8', 'Windows-1250', ''), 'LBR', 0, 'L');
                   }

                   $pdf->Ln();
                 }
           }

           else if($this->model == 'prazno')
           {
              $n = 0;
              while($n++ < $this->broj_redova)
              {
                $pdf->Cell(20, 18, iconv('UTF-8', 'Windows-1250', ''), 'LBR', 0, 'L');
                $pdf->Cell(150, 18, iconv('UTF-8', 'Windows-1250', ''), 'LBR', 0, 'L');

                if($predmet_razred = isset($this->predmets[$razred->getId()]) ? $this->predmets[$razred->getId()] : 0)
                foreach($predmet_razred as $predmet)
                {
                  $pdf->Cell(20, 18, iconv('UTF-8', 'Windows-1250', ''), 'LBR', 0, 'L');
                }

                $pdf->Ln();
              }
           }

           else if($this->model == 'clanarina')
           {
              if($ucenik_razred = isset($this->uceniks[$razred->getId()]) ? $this->uceniks[$razred->getId()] : 0)
                 foreach($ucenik_razred as $ucenik)
                 {
                   $pdf->Cell(20, 18, iconv('UTF-8', 'Windows-1250', $ucenik->getRedBr()), 'LBR', 0, 'L');
                   $pdf->Cell(150, 18, iconv('UTF-8', 'Windows-1250', $ucenik->joinIme()), 'LBR', 0, 'L');

                   if($predmet_razred = isset($this->predmets[$razred->getId()]) ? $this->predmets[$razred->getId()] : 0)
                   foreach($predmet_razred as $predmet)
                   {
                     $pdf->Cell(20, 18, iconv('UTF-8', 'Windows-1250', ''), 'LBR', 0, 'L');
                   }

                   $pdf->Ln();
                 }

              $n = count($ucenik_razred);
              while($n++ < $this->broj_redova)
              {
                $pdf->Cell(20, 18, iconv('UTF-8', 'Windows-1250', ''), 'LBR', 0, 'L');
                $pdf->Cell(150, 18, iconv('UTF-8', 'Windows-1250', ''), 'LBR', 0, 'L');

                if($predmet_razred = isset($this->predmets[$razred->getId()]) ? $this->predmets[$razred->getId()] : 0)
                foreach($predmet_razred as $predmet)
                {
                  $pdf->Cell(20, 18, iconv('UTF-8', 'Windows-1250', ''), 'LBR', 0, 'L');
                }

                $pdf->Ln();
              }
           }
        }

        return $pdf;
    }

}